package org.snail.proxy;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import org.snail.SnailClient;
import org.snail.transport.protocol.Request;

import java.lang.reflect.Method;

/**
 * 调用代理触发类
 *
 * Created by fish on 17/8/28.
 */
public class ProxySyncHandler {


    private SnailClient client;

    /**
     * 服务组别
     */
    private String group;

    /**
     * 服务名称
     */
    private String serviceProviderName;

    /**
     * 服务版本号
     */
    private String version;


    public ProxySyncHandler(SnailClient client,String group,String serviceProviderName,String version){
        this.client = client;
        this.group = group;
        this.serviceProviderName = serviceProviderName;
        this.version = version;
    }


    @RuntimeType
    public Object invoke(@Origin Method method, @AllArguments @RuntimeType Object[] args){
        //组装request
        Request request = new Request();
        request.setGroup(group);
        request.setServiceProviderName(serviceProviderName);
        request.setVersion(version);
        request.setMethodName(method.getName());
        request.setReturnType(method.getReturnType());
        request.setArgs(args);
        //负载均衡算法
        //钩子
        //拦截器
        //获取连接,临时测试
        Object object =  client.getDispatcher().invoke(client.next(),request,3000);
        return object;

    }

}
